package pers.lws.erp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.lws.erp.common.result.ResultBody;
import pers.lws.erp.entity.ProcessFile;
import pers.lws.erp.entity.ProductFile;
import pers.lws.erp.service.IProcessFileService;
import pers.lws.erp.utils.GeneratorID;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liuws
 * @since 2020-09-20
 */
@RestController
@RequestMapping("/processFile")
public class ProcessFileController {

    @Autowired
    private IProcessFileService processFileService;

    @GetMapping("/findProductProcess/{productId}")
    public ResultBody findProductProcess(@PathVariable String productId) {
        QueryWrapper<ProcessFile> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id", productId);
        List<ProcessFile> list = processFileService.list(wrapper);
        return list.isEmpty() ? ResultBody.error("产品还未设计工序！") : ResultBody.success("", list);
    }

    @RequestMapping("/show")
    public ResultBody show(){
        List<ProductFile> show = processFileService.show();
        return ResultBody.success("",show);
    }

    @RequestMapping("/show2")
    public ResultBody show2(){
        List<ProductFile> show2 = processFileService.show2();
        return ResultBody.success("",show2);
    }


    @RequestMapping("/getZz")
    public ResultBody getZz(@RequestBody String id){
        System.out.println("proId:"+id);
        ProcessFile zz = processFileService.getZz(id);
        System.out.println("值:"+processFileService.getZz(id));

        return ResultBody.success("",zz);
    }

    @RequestMapping("/getBz")
    public ResultBody getBz(@RequestBody String id){
        System.out.println("proId:"+id);
        ProcessFile zz = processFileService.getBz(id);
        System.out.println("值:"+processFileService.getBz(id));
        /*if(zz.size() == 0){
            System.out.println("啊啊啊啊啊啊啊啊啊啊啊");
            zz = null;
        }else{
            System.out.println("zzzzzzzzzz");
        }*/
        return ResultBody.success("",zz);
    }

    @RequestMapping("/sh")
    public ResultBody sh(@RequestBody String id){
        System.out.println(id);
        int a = processFileService.sh(id);
        return ResultBody.success("",a);
    }

    @RequestMapping("/sh2")
    public ResultBody sh2(@RequestBody String id){
        System.out.println(id);
        int a = processFileService.sh2(id);
        return ResultBody.success("",a);
    }


    @RequestMapping("/show3")
    public ResultBody show3(){
        System.out.println("aaaaaaaaaaaaaaaaaaa");
       List<ProcessFile>  list = processFileService.show3();
        System.out.println(list);
        return ResultBody.success("",list);
    }

    @RequestMapping("/add")
    public ResultBody add(@RequestBody ProcessFile form){
        int a = 0;
        int b = 0;
        int c = 0;
        //System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
        String gid = processFileService.getId();
        //System.out.println(gid);
        String gg = GeneratorID.getId(gid);
        //System.out.println(gg);
        System.out.println("输出前台数据");
        System.out.println(form);
        form.setProcessId(gg);
       // System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
        System.out.println(form);
        if(form.getAssembleManHour() != null && form.getAssembleManHour() != "" &&
                form.getAssembleUnitPrice() != null && form.getAssembleUnitPrice() != "" &&
                form.getPackManHour() != null && form.getPackManHour() != "" &&
                form.getPackUnitPrice() != null && form.getPackUnitPrice() != ""){

            System.out.println("组装包装");
            b = processFileService.add(form);
            String gid2 = processFileService.getId();
            System.out.println("gid2:"+gid2);
            String gg2 = GeneratorID.getId(gid2);
            System.out.println(gg2);
            System.out.println(form);
            form.setProcessId(gg2);
            c = processFileService.add2(form);

            System.out.println("执行修改状态");
            System.out.println("输出要修改状态的id："+form.getProductId());
            String abc = form.getProductId();
            int aa = processFileService.updProcessCheck(abc);
            System.out.println("修改状态结果："+aa);
            if(b == 1 && c == 1){
                a = 1;
            }else{
                a = 0;
            }
        }else if((form.getAssembleManHour() == null || form.getAssembleManHour() == "") &&
                (form.getAssembleUnitPrice() == null || form.getAssembleUnitPrice() == "") &&
                form.getPackManHour() != null && form.getPackManHour() != "" &&
                form.getPackUnitPrice() != null && form.getPackUnitPrice() != ""){
                System.out.println("包装");
            a = processFileService.add2(form);
        }else if(form.getAssembleManHour() != null && form.getAssembleManHour() != "" &&
                form.getAssembleUnitPrice() != null && form.getAssembleUnitPrice() != "" &&
                (form.getPackManHour() == null || form.getPackManHour() == "") &&
                (form.getPackUnitPrice() == null || form.getPackUnitPrice() == "")){
            System.out.println("组装");
            a = processFileService.add(form);
        }else{
            System.out.println("至少添加一个工序");
            a = 0;
        }

        return ResultBody.success("",a);
    }


}

